﻿using System;
using System.Text;
using System.Collections.Generic;
using System.Linq;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using DynORM.Interfaces;
using DynORM.Mappers;
using DynORM.Enums;
using DynORM;
using DynORM.ValueObjects;
using DynORM.ReferenceObjects;
using System.Xml.Serialization;
using System.Xml;
using System.IO;

namespace UnitTests.DAL
{
    [TestClass]
    public class ForeignKeySerializationUnitTests
    {
        private ForeignKey GenerateNewForeignKey(string tableName, string relTableName, string name)
        {
            Dictionary<int, IFieldDescriptor> fieldDescs = new Dictionary<int, IFieldDescriptor>();
            fieldDescs.Add(0, new FieldDescriptor(typeof(Guid?), "ForeignRowID_1"));
            fieldDescs.Add(1, new FieldDescriptor(typeof(Guid?), "ForeignRowID_2"));
            fieldDescs.Add(2, new FieldDescriptor(typeof(Guid?), "ForeignRowID_3"));
            Dictionary<int, IFieldDescriptor> relFieldDescs = new Dictionary<int, IFieldDescriptor>();
            relFieldDescs.Add(0, new FieldDescriptor(typeof(Guid), "RowID_1"));
            relFieldDescs.Add(1, new FieldDescriptor(typeof(Guid), "RowID_2"));
            relFieldDescs.Add(2, new FieldDescriptor(typeof(Guid), "RowID_3"));
            ForeignKeyDescriptor fkd = new ForeignKeyDescriptor(tableName, fieldDescs, relTableName, relFieldDescs, name);
            return new ForeignKey(fkd, null, null, null, null, null, null);
        }

        private string XmlXPathRunner(string xml, string xpath)
        {
            //intencially use XPath here in order to make this test independed from formatters
            XmlDocument doc = new XmlDocument();
            doc.LoadXml(xml);

            //XmlNamespaceManager nsmgr = new XmlNamespaceManager(doc.NameTable);
            //nsmgr.AddNamespace("ns", "http://schemas.datacontract.org/2004/07/IEOServerJobWebAPI.Auxiliary");
            //nsmgr.AddNamespace("i", "http://www.w3.org/2001/XMLSchema-instance");

            //XmlNode node = doc.SelectSingleNode(xpath, nsmgr);
            XmlNode node = doc.SelectSingleNode(xpath);

            return node.InnerText;
        }

        [TestMethod]
        public void SerializeForeignKeyTest()
        {
            using (MemoryStream ms = new MemoryStream())
            {
                XmlTextWriter writer = new XmlTextWriter(ms, UTF8Encoding.Default);

                XmlSerializer formatter = new XmlSerializer(typeof(ForeignKey));
                ForeignKey pk = GenerateNewForeignKey("TestTableName", "RelTestTableName", "TestForeignKey");
                formatter.Serialize(writer, pk);

                ms.Position = 0;
                StreamReader sr = new StreamReader(ms);
                string xml = sr.ReadToEnd();
            }
            //Assert.AreEqual(xml, @"");
        }

        [TestMethod]
        public void DeserializeForeignKeyTest()
        {
            ForeignKey fk;
            using (MemoryStream ms = new MemoryStream())
            {
                XmlTextWriter writer = new XmlTextWriter(ms, UTF8Encoding.Default);

                XmlSerializer formatter = new XmlSerializer(typeof(ForeignKey));
                fk = GenerateNewForeignKey("TestTableName", "RelTestTableName", "TestForeignKey");
                formatter.Serialize(writer, fk);
                fk = null;

                ms.Position = 0;
                XmlTextReader reader = new XmlTextReader(ms);
                fk = (ForeignKey)formatter.Deserialize(reader);
                Assert.IsNotNull(fk);
            }

            Assert.AreEqual("TestTableName", fk.TableName);
            Assert.AreEqual("TestForeignKey", fk.Name);
            Assert.AreEqual<int>(3, fk.Fields.Count());
            Assert.AreEqual<int>(3, fk.RelatedFields.Count());
        }

    }
}
